/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.plugins.videoplayer;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.zyuan.lawyerport.R;


/**
 * 播放器
 */
public class PlayVideoActivity extends Activity {
	private static final String TAG = "PlayVideoActivity";
	private LinearLayout header;
	private FrameLayout frameLayout;
	private WebView webView;
	private WebChromeClient chromeClient;
	private View myView;
	private WebChromeClient.CustomViewCallback myCallBack;

	public PlayVideoActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tool_play_video);
		header = (LinearLayout) findViewById(R.id.header);
		frameLayout = (FrameLayout) findViewById(R.id.framelayout);
		webView = (WebView) findViewById(R.id.webview);

		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.setWebViewClient(new MyWebviewCient());
		chromeClient = new MyChromeClient();
		webView.setWebChromeClient(chromeClient);
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.getSettings().setLayoutAlgorithm(
				WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		webView.setHorizontalScrollBarEnabled(false);
		webView.setVerticalScrollBarEnabled(false);

		final String USER_AGENT_STRING = webView.getSettings()
				.getUserAgentString() + " Rong/2.0";
		webView.getSettings().setUserAgentString(USER_AGENT_STRING);
		webView.getSettings().setSupportZoom(false);
		webView.getSettings().setPluginState(WebSettings.PluginState.ON);
		webView.getSettings().setLoadWithOverviewMode(true);

		// webView.loadUrl("http://look.appjx.cn/mobile_api.php?mod=news&id=12604");
		webView.loadUrl("http://www.rootshealth.com.cn/appvideo.html");

		if (savedInstanceState != null) {
			webView.restoreState(savedInstanceState);
		}

	}

	@Override
	public void onBackPressed() {
		if (myView == null) {
			super.onBackPressed();
		} else {
			chromeClient.onHideCustomView();
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		webView.saveState(outState);
	}

	public void addJavaScriptMap(Object obj, String objName) {
		webView.addJavascriptInterface(obj, objName);
	}

	public class MyWebviewCient extends WebViewClient {
		@Override
		public WebResourceResponse shouldInterceptRequest(WebView view,
                                                          String url) {
			WebResourceResponse response = null;
			response = super.shouldInterceptRequest(view, url);
			return response;
		}
	}

	public class MyChromeClient extends WebChromeClient {
		public boolean shouldOverrideUrlLoading(final WebView view,
				final String url) {

			return true;
		}

		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			if (myView != null) {
				callback.onCustomViewHidden();
				return;
			}
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

			frameLayout.addView(view);
			myView = view;
			myCallBack = callback;
			frameLayout.setVisibility(View.VISIBLE);
			header.setVisibility(View.GONE);
		}

		@Override
		public void onHideCustomView() {
			if (myView == null) {
				return;
			}
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			myCallBack.onCustomViewHidden();
			frameLayout.removeAllViews();
			frameLayout.setVisibility(View.GONE);
			webView.setVisibility(View.VISIBLE);
			header.setVisibility(View.VISIBLE);
			myView = null;
			// webView.setWebChromeClient(chromeClient);
			// webView.setWebViewClient(new MyWebviewCient());
		}

		@Override
		public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
			// TODO Auto-generated method stub
			Log.d("ZR",
					consoleMessage.message() + " at "
							+ consoleMessage.sourceId() + ":"
							+ consoleMessage.lineNumber());
			return super.onConsoleMessage(consoleMessage);
		}
	}

	/**
	 * 判断是否是全屏
	 *
	 * @return
	 */
	public boolean inCustomView() {
		return (myView != null);
	}

	/**
	 * 全屏时按返加键执行退出全屏方法
	 */
	public void hideCustomView() {
		chromeClient.onHideCustomView();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onResume() {
		super.onResume();
		webView.onResume();
		webView.resumeTimers();
		/**
		 * 设置为横屏
		 */
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		webView.onPause();
		webView.pauseTimers();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		super.onDestroy();
		frameLayout.removeAllViews();
		webView.loadUrl("about:blank");
		webView.stopLoading();
		webView.setWebChromeClient(null);
		webView.setWebViewClient(null);
		webView.destroy();
		webView = null;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (inCustomView()) {
				// webView.loadUrl("about:blank");
				hideCustomView();
				return true;
			} else {
				webView.loadUrl("about:blank");
				PlayVideoActivity.this.finish();
			}
		}
		return false;
	}

}